package net.opentsdb.core;

import java.util.Map;

/**
 * @author Hujf
 * @title: IncomingDataPoints
 * @date 2021-09-22 14:00
 * @description: 数据处理
 */
public class IncomingDataPoints {
    public static void checkMetricAndTags(String metric, Map<String, String> tags) {
    }

    public static byte[] rowKeyTemplate(TSDB tsdb, String metric,Map<String, String> tags) {
        final short metric_width = tsdb.metrics.width();
        final short tag_name_width = tsdb.tag_names.width();
        final short tag_values_width = tsdb.tag_values.width();
        final short num_tags = (short) tags.size();

        int row_size = Const.SALT_WIDTH()+metric_width+Const.TIMESTAMP_BYTES+tag_name_width* num_tags+tag_values_width*num_tags;
        final byte[] row = new byte[row_size];

        short pos = (short) Const.SALT_WIDTH();

        copyInRowKey(row,pos,(tsdb.getConfig().isAuto_metric()?tsdb.metrics.getOrCreateId(metric):tsdb.metrics.getId(metric)));

        pos += metric_width;
        pos+=Const.TIMESTAMP_BYTES;

        for(byte[] tag:Tags.resolveOrCreateAll(tsdb,tags)){
            copyInRowKey(row,pos,tag);
            pos+=tag.length;
        }
        return row;
    }

    private static void copyInRowKey(byte[] row, short pos, byte[] bytes) {
        System.arraycopy(bytes,0,row,pos,bytes.length);
    }
}
